/*
 * 文件名：DeviceCotController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wangw
 * 修改时间：2020年2月19日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.DeviceCotDto;
import top.infopub.mgr.bpa.domain.DeviceCotDomain;
import top.infopub.mgr.bpa.domain.DeviceLedgerDomain;
import top.infopub.mgr.bpa.service.DeviceCotService;
import top.infopub.mgr.bpa.service.EffectPlanDetailService;
import top.infopub.mgr.bpa.service.EffectPlanService;
import top.infopub.mgr.bpa.service.InnerAgrtService;
import top.infopub.mgr.bpa.vo.ContractManageVo;
import top.infopub.mgr.bpa.vo.DeviceCotVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.MarketManageService;
import top.infopub.mgr.sg.service.CoopInfoService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.bpa.Basic;
import top.infopub.model.bpa.DeviceCot;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author taiszzz
 * @version 2020年2月19日
 * @see DeviceCotController
 * @since  设备合同评审
 */
@Controller
@RequestMapping("/deviceCot")
public class DeviceCotController extends GenericFlowController {

    @Autowired
    private OrgInfoService orgInfoService;
    private OAFlowService oaFlowService;
    @Autowired
    private MarketManageService marketManageService;
    @Autowired
    private DeviceCotService deviceCotService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private InnerAgrtService innerAgrtService;
    
    @Autowired
    private CoopInfoService coopInfoService;
    
    @Autowired
    private EffectPlanService effectPlanService;

    
    @Autowired
    private EffectPlanDetailService effectPlanDetailService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id) {
        request.setAttribute("id",id);
        return "bpa/lay-EquipmentContractReview-detail";
    }
    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        //获取登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //查询区域公司列表
        List<OrgInfo> orgInfoList = marketManageService.queryOrgInfoByAuthUserId(vo);
        request.setAttribute("orgInfoList", orgInfoList);
        //查询营销人员列表
        List<StaffInfo> staffInfoList = marketManageService.queryStaffInfoByAuthUserId(vo);
        request.setAttribute("staffInfoList", staffInfoList);
        return "bpa/lay-deviceCot-list";
    }
    //下拉数据详情
    @RequestMapping("/projectBasic")
    @ResponseBody
    public Basic projectBasic(String proCode) {
        Basic basic =  deviceCotService.projectBasic(proCode);
        return basic;
    }
    /**
     * 设备合同列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<DeviceCotDomain> queryDataByCondition(DeviceCotVo deviceCotVo, ParamPage paramPage) throws ParseException {
        Page<DeviceCotDomain> page = new Page<DeviceCotDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (deviceCotVo.getEndDate() != null) {
            deviceCotVo.setEndDate(DateUtils.addDays(deviceCotVo.getEndDate(), 1));
        }
        page = deviceCotService.selectPageByDomainWhere(page, deviceCotVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
    //项目下拉框数据
    @RequestMapping("/projectDropDownBox")
    @ResponseBody
    public List<Map<String,String>> projectDropDownBox() {
        return deviceCotService.projectDropDownBox();
    }

    //农民工保证金承担方 居间费承担方 履约保函承担方的下拉接口
    @RequestMapping("/projectCompanyBox")
    @ResponseBody
    public List<Map<String,String>> projectCompanyBox() {
        return deviceCotService.projectCompanyBox();
    }



    /**
     * 设备合同台账
     *
     * @return
     */
    @RequestMapping("/selectByLedger")
    @ResponseBody
    public RspVo<DeviceLedgerDomain> selectByLedger(ContractManageVo contractManageVo, ParamPage paramPage) throws ParseException {
        Page<DeviceLedgerDomain> page = new Page<DeviceLedgerDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (contractManageVo.getEndDate() != null) {
            contractManageVo.setEndDate(DateUtils.addDays(contractManageVo.getEndDate(), 1));
        }
        page = deviceCotService.selectByLedger(page, contractManageVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }


    //详情
    @RequestMapping("/queryById")
    @ResponseBody
    public DeviceCotDto queryById(String id) {
        DeviceCotDto dao =  deviceCotService.queryById(id);
        return dao;
    }


    // 保存提交
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, String submitType) {
        if (StringUtils.isEmpty(bodyStr)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String object = StringEscapeUtils.unescapeJava(bodyStr);
        DeviceCotDto dto = JSON.parseObject(object, DeviceCotDto.class);
        Basic basic = dto.getBasic();
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(dto.getFileAtt()!=null){
            String[] fileAtt = dto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo,basic.getProCode(),file);
                attachmentList.add(attachment);
            }
        }

        if ("0".equals(submitType)) {//0是保存
            int i = deviceCotService.save(dto,attachmentList);
            if (i > 0) {
                return RspVo.getSuccessResponse(0, "保存成功");
            } else {
                return RspVo.getFailureResponse(0, "保存失败");
            }
        } else { //1是提交
            //判断状态
            DeviceCot deviceCot  = deviceCotService.findById(basic.getId());
            if (deviceCot!=null)
            {
                String processStatus = deviceCot.getProcessStatus();
                if ("1".equals(processStatus) || "9".equals(processStatus))
                {
                    return RspVo.getFailureResponse(0, "审批中或审批已完成，修改失败");
                }
            }

//            if (StringUtils.isNotBlank(deviceCot.getOaId())) {
//                oaFlowService.finishOA(deviceCot.getOaId());
//            }
            //提交流程  先保存
            int i = deviceCotService.save(dto,attachmentList);
            if (i > 0) {//保存成的情况 走流程
                //发起流程
                /*String process = proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE15, object, basic.id, code, vo);*/
                /*保存对内合同金额合计*/

                return RspVo.getFailureResponse(0, "提交成功");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }
        }
    }




}
